package util.exact;

import bean.State;

import java.util.HashSet;
import java.util.Iterator;

public class UpdateStates {

    public static HashSet<State> updateStates(HashSet<State> tempStates,HashSet<State> states) {
        HashSet<State> statesNew = new HashSet<>();

        //先考虑第一种情况，即：states为空，此时只需要把tempStates中的所有state复制到statesNew中即可,最后返回statesNew
        if ( states.size() == 0 ) {
            Iterator<State> iteratorTempStates = tempStates.iterator();
            while ( iteratorTempStates.hasNext() ) {
                statesNew.add(iteratorTempStates.next());
            }
            return statesNew;
        }else {
            /*
            TODO 还没有想好应该怎么更新states，目前的方法测试发现有问题，并且适用情况比较窄，目前
             只考虑了w2加入tempstates的情况
             */
            //遍历tempStates，states
            if ( tempStates.size() != 0 ) {
                for (State tempState : tempStates) {
                    if ( states.size() != 0 ) {
                        for (State state : states) {
                            //如果两个state相同，则statesNew中加入cost低的state
                            if ( IsSameState.isSameState(tempState,state) ) {
                                if ( tempState.getCost()<state.getCost() ) {
                                    statesNew.add(tempState);
                                }else {
                                    statesNew.add(state);
                                }
                            }else { //此时两个state不相同
                                if (tempState.getSkills().size() == 1 ) {
                                    statesNew.add(state);
                                }else {
                                    statesNew.add(tempState);
                                }
                            }
                        }
                    }
                }
            }
            return statesNew;

        }


    }
}
